package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;

import javax.swing.JCheckBox;

import panel.GlyphPanel;


public class EnhanceContrastOptionDialog extends JDialog implements
		ActionListener, WindowListener, Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JPanel optionPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JLabel saturatedPixelsLabel = null;
	private JTextField saturatedTextField = null;
	private JCheckBox normalizeCheckBox = null;
	private JCheckBox equalizeCheckBox = null;
	private String saturated;
	private boolean normalize;
	private boolean equalize;
	private GlyphPanel panel;

	/**
	 * @param owner
	 */
	public EnhanceContrastOptionDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel = panel;
		this.addWindowListener(this);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.insets = new Insets(0, 10, 10, 10);
			gridBagConstraints.gridy = 0;
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints1);
			buttonPanel.add(getCancelButton(), gridBagConstraints);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes optionPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOptionPanel() {
		if (optionPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridy = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 2;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridy = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints3.gridx = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
			gridBagConstraints2.insets = new Insets(0, 20, 10, 10);
			gridBagConstraints2.gridy = 0;
			saturatedPixelsLabel = new JLabel();
			saturatedPixelsLabel.setText("Saturated Pixels (%)");
			optionPanel = new JPanel();
			optionPanel.setLayout(new GridBagLayout());
			optionPanel.add(saturatedPixelsLabel, gridBagConstraints2);
			optionPanel.add(getSaturatedTextField(), gridBagConstraints3);
			optionPanel.add(getNormalizeCheckBox(), gridBagConstraints4);
			optionPanel.add(getEqualizeCheckBox(), gridBagConstraints5);
		}
		return optionPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);
		}
		return cancelButton;
	}

	/**
	 * This method initializes saturatedTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSaturatedTextField() {
		if (saturatedTextField == null) {
			saturatedTextField = new JTextField();
			saturatedTextField.setPreferredSize(new Dimension(120, 20));
			saturatedTextField.setText("0.5");
		}
		return saturatedTextField;
	}

	/**
	 * This method initializes normalizeCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getNormalizeCheckBox() {
		if (normalizeCheckBox == null) {
			normalizeCheckBox = new JCheckBox();
			normalizeCheckBox.setText("Normailze");
		}
		return normalizeCheckBox;
	}

	/**
	 * This method initializes equalizeCheckBox
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getEqualizeCheckBox() {
		if (equalizeCheckBox == null) {
			equalizeCheckBox = new JCheckBox();
			equalizeCheckBox.setText("Equalize Histogram");
		}
		return equalizeCheckBox;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			saturated = saturatedTextField.getText();
			normalize = normalizeCheckBox.isSelected();
			equalize = equalizeCheckBox.isSelected();
			String command = String.format(
					"run(\"Enhance Contrast\", \"saturated=%s", saturated);
			if (normalize)
				command = String.format("%s normalize", command);
			if (equalize)
				command = String.format("%s equalize", command);
			command = String.format("%s\");", command);
			panel.setOptionsSet(true);
			panel.setDialog(this);
			panel.setCommand(command);

		} else {
			saturatedTextField.setText(saturated);
			normalizeCheckBox.setSelected(normalize);
			equalizeCheckBox.setSelected(equalize);
		}
		this.setVisible(false);
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		saturatedTextField.setText(saturated);
		normalizeCheckBox.setSelected(normalize);
		equalizeCheckBox.setSelected(equalize);
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e) {
		saturated = saturatedTextField.getText();
		normalize = normalizeCheckBox.isSelected();
		equalize = equalizeCheckBox.isSelected();

	}

}
